#include "stdafx.h"
#include "XMenuItemImpl.h"

#include "XControlImpl.cpp"
#include "XFrameImpl.cpp"

#include <UIUtil.h>
#include <MenuUIDef.h>

IMPLEMENT_SKIN_CLASS(CXMenuItemImpl);

//////////////////////////////////////////////////////////////////////////
// 
CXMenuItemImpl::CXMenuItemImpl()
{
    m_bChecked = FALSE;
}

CXMenuItemImpl::~CXMenuItemImpl()
{
    ;
}

//////////////////////////////////////////////////////////////////////////
// IXMenuItem

HRESULT CXMenuItemImpl::put_Check(IN BOOL bCheck)
{
    m_bChecked = bCheck;
    return S_OK;
}

HRESULT CXMenuItemImpl::get_Check(OUT BOOL* pbCheck)
{
    *pbCheck = m_bChecked;
    return S_OK;
}

HRESULT CXMenuItemImpl::put_Enable(IN BOOL bEnable)
{
    SetEnable(bEnable);
    return S_OK;
}

HRESULT CXMenuItemImpl::get_Enable(OUT BOOL* pbEnable)
{
    *pbEnable = IsEnable();
    return S_OK;
}

HRESULT CXMenuItemImpl::put_TextColor(IN COLORREF clrText)
{
    UIUtil::XGetTextEnv(dynamic_cast<IXControl*>(this))->put_TextColor(clrText);
    return S_OK;
}

HRESULT CXMenuItemImpl::get_TextColor(OUT COLORREF* pClrText)
{
    *pClrText = UIUtil::XGetTextEnv(dynamic_cast<IXControl*>(this))->get_TextColor();
    return S_OK;
}

IXMenuEx* CXMenuItemImpl::GetSubMenu()
{
    return NULL;
}
//////////////////////////////////////////////////////////////////////////

BOOL CXMenuItemImpl::OnClicked(const POINT* pt)
{
    BOOL bResult = superclass::OnClicked(pt);
    return bResult;
}

BOOL CXMenuItemImpl::OnMouseHover(POINT* pt)
{
    BOOL bResult = superclass::OnMouseHover(pt);
    Invalidate();
    return bResult;
}

BOOL CXMenuItemImpl::OnMouseLeave(POINT* pt)
{
    BOOL bResult = superclass::OnMouseLeave(pt);
    Invalidate();
    return bResult;
}

//////////////////////////////////////////////////////////////////////////
// IXControl

void CXMenuItemImpl::GetAutoSize(OUT LPSIZE pAutoSize)
{
    SIZE sizeText;
    {
        IXFont* pFont = NULL;
        IXTextEnv* pTextEnv = NULL;

        get_Font(&pFont);
        get_TextEnv(&pTextEnv);

        SIZE sizeMax = {0};
        get_MaxSize(&sizeMax);
        sizeText.cx = (sizeMax.cx == -1) ? GetWidth() : sizeMax.cx;
        sizeText.cy = (sizeMax.cy == -1) ? GetHeight() : sizeMax.cy;
        DrawUtil::XGetTextSize(m_strText.c_str(), m_strText.Length(), &sizeText, pFont, pTextEnv);
    }

    SIZE sizeIcon = g_sizeIcon;

    pAutoSize->cx = sizeText.cx + sizeIcon.cx + g_nIconTextMargin + g_rcItemBkgMargin.left + g_rcItemBkgMargin.right;
    pAutoSize->cy = (sizeText.cy > sizeIcon.cy) ? sizeText.cy : sizeIcon.cy;
}

void CXMenuItemImpl::Draw(XCANVAS xCanvas, const RECT& rcDraw)
{
    superclass::Draw(xCanvas, rcDraw);

    // Status
    BOOL bIsHover = TestControlFlags(ESkin_Hover);
    BOOL bEnable = IsEnable();

    RECT rcWnd;
    get_WindowRect(&rcWnd);

    // DrawBackground
    RECT rcBkg = rcWnd;
    rcBkg.left += g_rcItemBkgMargin.left;
    rcBkg.top += g_rcItemBkgMargin.top;
    rcBkg.right -= g_rcItemBkgMargin.right;
    rcBkg.bottom -= g_rcItemBkgMargin.bottom;
    DrawUtil::FillRect(xCanvas, &rcBkg, g_clrItemBkg);

    // DrawIcon
    RECT rcIcon = {0};
    rcIcon.top = (rcWnd.top + g_rcItemBkgMargin.top + rcWnd.bottom - g_rcItemBkgMargin.bottom) / 2 + rcWnd.top;
    rcIcon.bottom = rcIcon.top + g_sizeIcon.cy;

    IXDraw* pIcon = NULL;
    if(bIsHover)
        get_HoverDraw(&pIcon);
    if(!bIsHover || pIcon == NULL)
        get_Draw(&pIcon);

    if(pIcon == NULL)
    {
        rcIcon.right = rcIcon.left;
    }
    else
    {
        rcIcon.right = rcIcon.left + g_sizeIcon.cx;
        pIcon->Draw(xCanvas, &rcIcon);
    }

    // DrawText
    RECT rcText = rcWnd;
    rcText.left = rcIcon.right + g_nIconTextMargin;
    rcText.right -= g_rcItemBkgMargin.right;
    rcText.top += g_rcItemBkgMargin.top;
    rcText.bottom -= g_rcItemBkgMargin.bottom;

    IXFont* pFont = NULL;
    IXTextEnv* pTextEnv = NULL;

    get_Font(&pFont);
    get_TextEnv(&pTextEnv);

    DrawUtil::XDrawText(xCanvas, m_strText.c_str(), m_strText.Length(), &rcText, pTextEnv, pFont);
}
